************************************************************************************************************************
*                        PREPARE DATA FOR AND RUN TRAD MEDICARE REGRESSIONS WITH AND W/OUT PATIENT FEs                 *
************************************************************************************************************************

*--------------------------------------------------
log using "${SIDCodePath}/sidsedd_regressions_trad.log", replace
use "${SIDDataPath}/sidsedd_merged_20102016.dta", clear
//-------------------------------------------------------//
// REGRESSIONS WITH PATIENT CHARACTERISTIC FIXED EFFECTS //
//-------------------------------------------------------//
	/***** MEDICARE RESULTS *****/
		// Sample: Medicare inpatient + outpatient
			// Dep vars: inpatient status for 1-5 hours before/after midnight
				
				foreach var of varlist		inpatient nxtvi_30d{
					forval t = 1/5 {
						preserve
							keep	if trad_medicare == 1
							keep 	if hourstilMN`t' == 1

							reghdfe `var' post_qtr_after_MN, ///
									absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
									vce(cluster edhour_2 year_dqtr)
							eststo 	med_`var'_`t'h_diRD, ///
									title(proper("`var'"))

							estadd   local hospitaltimeFE "X"
							estadd   local patientFE 	  "X"
							estadd 	 local MNsample 	  "`t'"
							estadd 	 local pop 			  "IP & OP"
							estadd   local medsample 	  "All"
						restore

					}
					esttab ///
						using "${SIDOutputPath}/reg_trad_patientFEs/reg_med_`var'_byhours_diRD.tex", replace /// 
						star(* .10 ** .05 *** .01) ///
						scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
						coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
						b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
						addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour" "Patient FE: age-sex bin, race, hispanic, point of origin, last ED visit 30 days, n. chronic conditions, zip code income" )

					eststo clear 
				}  
			
			// Subset to vulnerable patients
			// Dep vars: inpatient status for 1-5 hours before/after midnight
				
				foreach var of varlist		inpatient nxtvi_30d{
					forval t = 3/3 {
						preserve
							keep	if trad_medicare == 1
							keep 	if hourstilMN`t' == 1


							// Old -- top quartile
								qui sum age, detail
								local top_p25 = r(p75)

								reghdfe `var' post_qtr_after_MN if age >= `top_p25', ///
										absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
										vce(cluster edhour_2 year_dqtr)
										
								eststo 	old_`var'_`t'h_diRD, ///
										title(proper("`var'"))

								estadd   local hospitaltimeFE "X"
								estadd   local patientFE 	  "X"
								estadd 	 local MNsample 	  "`t'"
								estadd 	 local pop 			  "IP \& OP"
								estadd   local medsample 	  "Top quartile age"

							// Many chronic conditions -- top quartile
								qui sum nchronic, detail
								local top_p25 = r(p75)

								reghdfe `var' post_qtr_after_MN if nchronic >= `top_p25', ///
										absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
										vce(cluster edhour_2 year_dqtr)
										
								eststo 	cc_`var'_`t'h_diRD, ///
										title(proper("`var'"))

								estadd   local hospitaltimeFE "X"
								estadd   local patientFE 	  "X"
								estadd 	 local MNsample 	  "`t'"
								estadd 	 local pop 			  "IP \& OP"
								estadd   local medsample 	  "Top quartile n cc's"


							// Non-white
							reghdfe `var' post_qtr_after_MN if race2 != 5, ///
										absorb(edhour_2 year_dqtr dshospid agebin##female hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
										vce(cluster edhour_2 year_dqtr)
										
								eststo 	black_`var'_`t'h_diRD, ///
										title(proper("`var'"))

								estadd   local hospitaltimeFE "X"
								estadd   local patientFE 	  "X"
								estadd 	 local MNsample 	  "`t'"
								estadd 	 local pop 			  "IP \& OP"
								estadd   local medsample 	  "Non-white"

							// Low income -- lowest quartile
							reghdfe `var' post_qtr_after_MN if medincstq == 1, ///
										absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2) /// 
										vce(cluster edhour_2 year_dqtr)
										
								eststo 	lowinc_`var'_`t'h_diRD, ///
										title(proper("`var'"))

								estadd   local hospitaltimeFE "X"
								estadd   local patientFE 	  "X"
								estadd 	 local MNsample 	  "`t'"
								estadd 	 local pop 			  "IP \& OP"
								estadd   local medsample 	  "Bottom quartile zip code income"

						restore

						esttab ///
							using "${SIDOutputPath}/reg_trad_patientFEs/reg_med_`var'_bysubset_diRD.tex", replace /// 
							star(* .10 ** .05 *** .01) ///
							scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
							coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
							b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
							addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour" "Patient FE: age-sex bin, race, hispanic, point of origin, last ED visit 30 days, n. chronic conditions, zip code income" )

						eststo clear 

					}
					
				} 


			
			// Dep vars: inpatient + outpatient outcomes
				 foreach var of varlist		inpatient out obs totchg chg_ED ndx npr orproc nxtvi* {
					forval t = 3/3 {
						preserve
							keep 	if trad_medicare == 1 
							keep 	if hourstilMN`t' == 1

							reghdfe `var' post_qtr_after_MN, /// 
									absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
									vce(cluster edhour_2 year_dqtr)
							eststo 	med_`var'_`t'h_diRD, /// 
									title(proper("`var'"))

							estadd  local hospitaltimeFE "X"
							estadd  local patientFE 	 "X"
							estadd 	local MNsample 	  	 "`t'"
							estadd 	local pop 			 "IP \& OP"
							estadd  local medsample 	 "All"
						restore

					}
				 
				}
				esttab ///
				 	using "${SIDOutputPath}/reg_trad_patientFEs/reg_med_`t'h_diRD.tex", replace /// 
					star(* .10 ** .05 *** .01) ///
					scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
					coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
					b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
					addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour" "Patient FE: age-sex bin, race, hispanic, point of origin, last ED visit 30 days, n. chronic conditions, zip code income" )
				eststo clear  
			
			
			
			// Dep var: after-MN likelihood and edhour == 11pm-midnight
			foreach var of varlist		after_MN edhour_11pm{
					forval t = 3/3 {
						preserve
							keep 	if trad_medicare == 1 
							keep 	if hourstilMN`t' == 1

							reghdfe `var' post_qtr, /// 
									absorb(dshospid) /// 
									vce(cluster year_dqtr)
							eststo 	med_`var'_`t'h_diRD, /// 
									title(proper("`var'"))

							estadd  local hospitaltimeFE ""
							estadd  local patientFE 	 ""
							estadd 	local MNsample 	  	 "`t'"
							estadd 	local pop 			 "IP \& OP"
							estadd  local medsample 	 "All"
						restore

					}
				}
				 esttab ///
				 	using "${SIDOutputPath}/reg_trad_nopatientFEs/reg_med_afterMN_edhour_11pm_3h_diRD.tex", replace /// 
					star(* .10 ** .05 *** .01) ///
					scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
					coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
					b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
					addnotes("Hospital-time FE: hospital" )
				eststo clear 

	/***** NON-MEDICARE RESULTS *****/
		// Sample: non-Medicare inpatient + outpatient
			// Dep vars: inpatient status 
			 foreach var of varlist 	inpatient{
				forval t = 1/5 {
					preserve
						keep if trad_medicare == 0 
						keep if hourstilMN`t' == 1

						reghdfe `var' post_qtr_after_MN, /// 
								absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
								vce(cluster edhour_2 year_dqtr)
						eststo  nmed_`var'_`t'h_diRD, ///
								title(proper("`var'"))

						estadd  local hospitaltimeFE "X"
						estadd  local patientFE 	 "X"
						estadd  local MNsample 		"`t'"
						estadd  local pop 			"IP & OP"
						estadd  local medsample 	"Non"
					restore

					}
				} 
				esttab /// 
					using "${SIDOutputPath}/reg_trad_patientFEs/reg_nmed_byhours_diRD.tex", replace /// 
					star(* .10 ** .05 *** .01) ///
					scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations" ) /// 
					coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
					b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
					addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour" "Patient FE: age-sex bin, race, hispanic, point of origin, last ED visit 30 days, n. chronic conditions, zip code income" )
				eststo clear  

			

log close

